# 电子病历管理的视图文件

from django.shortcuts import render, redirect
from common import models



def index(request):
    '''浏览信息'''
    umod1 = models.Patientinfo.objects
    ulist1 = umod1.all()
    context = {"patientinfolist": ulist1}
    return render(request, "myapp/Medrecordinfo/index.html", context)


def search(request):
    # 从表单中获取要添加的信息并封装到obj对象中
    hspid = request.POST['hspid']
    obj = models.Medrecordinfo.objects.get(hspid=hspid)
    context = {'Medrecordinfo': obj}
    return render(request, "myapp/Medrecordinfo/edit.html", context)


def add(request):
    '''加载信息添加表单'''
    if request.method == "GET":
        return render(request, "myapp/Medrecordinfo/add.html")
    # 获取用户POST提交过来的数据
    request.POST.get("hspid", "zhusu", "xianbingshi", "jiwangshi", "hunyushi", "yuejingshi", "jiazushi", "tigejiancha",
                     "zhuankejiancha", "fuzhujiancha", "jianbiezhenduan", "chubuzhenduan", "zhenliaojihua")
    # 保存到数据库
    models.Medrecordinfo.objects.create()

    return redirect("Medrecordinfo/")


def insert(request):
    '''执行信息添加'''
    try:
        obj = models.Medrecordinfo()
        # 从表单中获取要添加的信息并封装到ob对象中
        obj.hspid = request.POST['hspid']
        obj.zhusu = request.POST['zhusu']
        obj.xianbingshi = request.POST['xianbingshi']
        obj.jiwangshi = request.POST['jiwangshi']
        obj.hunyushi = request.POST['hunyushi']
        obj.yuejingshi = request.POST['yuejingshi']
        obj.jiazushi = request.POST['jiazushi']
        obj.tigejiancha = request.POST['tigejiancha']
        obj.zhuankejiancha = request.POST['zhuankejiancha']
        obj.fuzhujiancha = request.POST['fuzhujiancha']
        obj.jianbiezhenduan = request.POST['jianbiezhenduan']
        obj.chubuzhenduan = request.POST['chubuzhenduan']
        obj.zhenliaojihua = request.POST['zhenliaojihua']
        obj.save()
        context = {'info': "添加成功！"}
    except Exception as err:
        print(err)
        context = {'info': "添加失败！"}
    return render(request, "myapp/info.html", context)


def delete(request, nid):
    try:
        obj = models.Medrecordinfo.objects.get(hspid=nid)
        obj.delete()
        context = {'info': "删除成功"}
    except Exception as err:
        print(err)
        context = {'info': "删除失败"}
    return render(request, "myapp/info.html", context)


def edit(request, hspid):
    '''加载信息编辑表单'''
    try:
        hspid=hspid
        patient = models.Patientinfo.objects.get(hspid=hspid)
        obj = models.Medrecordinfo.objects.get(hspid=hspid)
        #context = {'Patientinfo': patient}
        context = {'Patientinfo': patient,'Medrecordinfo':obj}
        return render(request, "myapp/Medrecordinfo/edit.html", context)
    except Exception as err:
        hspid = hspid
        patient = models.Patientinfo.objects.get(hspid=hspid)
        context = {'Patientinfo': patient}
        return render(request, "myapp/Medrecordinfo/edit.html", context)


def update(request):
    '''执行信息编辑'''
    try:
        hspid = request.POST['hspid']
        obj = models.Medrecordinfo.objects.get(hspid=hspid)
        obj.zhusu = request.POST['zhusu']
        obj.xianbingshi = request.POST['xianbingshi']
        obj.jiwangshi = request.POST['jiwangshi']
        obj.hunyushi = request.POST['hunyushi']
        obj.yuejingshi = request.POST['yuejingshi']
        obj.jiazushi = request.POST['jiazushi']
        obj.tigejiancha = request.POST['tigejiancha']
        obj.zhuankejiancha = request.POST['zhuankejiancha']
        obj.fuzhujiancha = request.POST['fuzhujiancha']
        obj.jianbiezhenduan = request.POST['jianbiezhenduan']
        obj.chubuzhenduan = request.POST['chubuzhenduan']
        obj.zhenliaojihua = request.POST['zhenliaojihua']
        obj.save()
        context = {'info': "修改成功！"}
    except Exception as err:
        print(err)
        context = {'info': "修改失败！"}
    return render(request, "myapp/info.html", context)


def patients(request):
    '''选择病人'''
    umod = models.Patientinfo.objects
    ulist = umod.all()
    context = {"patientinfolist": ulist}
    return render(request, "myapp/Medrecordinfo/patients.html", context)


def records(request, hspid):
    '''选择模板'''
    try:
        umod = models.Templateinfo.objects
        ulist = umod.all()
        patient = models.Patientinfo.objects.get(hspid=hspid)
        context = {'Templateinfolist': ulist, 'patient': patient}
        return render(request, "myapp/Medrecordinfo/records.html", context)
    except Exception as err:
        print(err)
        context = {'info': "没有找到相关模板！"}
        return render(request, "myapp/info.html", context)


def total(request, hspid, templateid):
    '''加载信息编辑表单'''
    try:
        obj = models.Templateinfo.objects.get(templateid=templateid)
        patient = models.Patientinfo.objects.get(hspid=hspid)
        context = {'Templateinfo': obj, 'Patientinfo': patient}
        return render(request, "myapp/Medrecordinfo/total.html", context)
    except Exception as err:
        print(err)
        context = {'info': "没有找到要修改的信息！"}
        return render(request, "myapp/info.html", context)


def update1(request):
    '''执行信息编辑'''
    try:
        hspid = request.POST['hspid']
        patient = models.Patientinfo.objects.get(hspid=hspid)
        obj = models.Medrecordinfo()
        patient.hspid = request.POST['hspid']
        patient.name = request.POST['name']
        patient.gender = request.POST['gender']
        patient.departname = request.POST['departname']
        patient.doctorname = request.POST['doctorname']
        obj.zhusu = request.POST['zhusu']
        obj.xianbingshi = request.POST['xianbingshi']
        obj.jiwangshi = request.POST['jiwangshi']
        obj.hunyushi = request.POST['hunyushi']
        obj.yuejingshi = request.POST['yuejingshi']
        obj.jiazushi = request.POST['jiazushi']
        obj.tigejiancha = request.POST['tigejiancha']
        obj.zhuankejiancha = request.POST['zhuankejiancha']
        obj.fuzhujiancha = request.POST['fuzhujiancha']
        obj.jianbiezhenduan = request.POST['jianbiezhenduan']
        obj.chubuzhenduan = request.POST['chubuzhenduan']
        obj.zhenliaojihua = request.POST['zhenliaojihua']
        obj.save()
        context = {'info':"修改成功！"}
        return render(request,"myapp/info.html",context)
    except Exception as err:
        print(err)
        context = {'info':"修改失败！"}
        return render(request,"myapp/info.html",context)
